package com.adams.app.ui;

import java.text.DecimalFormat;
import java.util.Calendar;

import com.adams.app.R;
import com.adams.app.listener.StepDetector;
import com.adams.app.service.StepService;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

@SuppressLint("HandlerLeak")
public class StepActivity extends BaseActivity {

	private TextView tv_show_step;// 步数
	private TextView tv_week_day;// 星期
	private TextView tv_date;// 日期

	private TextView tv_timer;// 运行时间

	private TextView tv_distance;// 行程
	private TextView tv_calories;// 卡路里
	private TextView tv_velocity;// 速度

	private Button btn_start;// 开始按钮
	private Button btn_stop;// 停止按钮

	// 星标
	private ImageView iv_star_1;
	private ImageView iv_star_2;
	private ImageView iv_star_3;
	private ImageView iv_star_4;
	private ImageView iv_star_5;
	private ImageView iv_star_6;
	private ImageView iv_star_7;
	private ImageView iv_star_8;
	private ImageView iv_star_9;
	private ImageView iv_star_10;

	private long timer = 0;// 运动时间
	private static long startTimer = 0;// 开始时间

	private static long tempTime = 0;

	private Double distance = 0.0;// 路程：米
	private Double calories = 0.0;// 热量：卡路里
	private Double velocity = 0.0;// 速度：米每秒

	private int step_length = 0;
	private int weight = 0;
	private int total_step = 0;

	private Thread thread;

	Handler handler = new Handler() {// Handler对象用于更新当前步数

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);

			countDistance();

			if (timer != 0 && distance != 0.0) {

				// 体重、距离
				// 跑步热量（kcal）＝体重（kg）×距离（公里）×1.036
				calories = weight * distance * 0.001;

				velocity = distance * 1000 / timer;
			} else {
				calories = 0.0;
				velocity = 0.0;
			}

			countStep();

			tv_show_step.setText(total_step + "");// 显示当前步数

			tv_distance.setText(formatDouble(distance));// 显示路程
			tv_calories.setText(formatDouble(calories));// 显示卡路里
			tv_velocity.setText(formatDouble(velocity));// 显示速度

			tv_timer.setText(getFormatTime(timer));// 显示当前运行时间

			changeStep();// 设置当前步数和星标

		}

		/**
		 * 设置当前步数和星标
		 */
		private void changeStep() {
			int level = StepDetector.CURRENT_SETP / 1000;
			switch (level) {
			case 10:
				iv_star_10.setImageResource(R.drawable.img_star);
			case 9:
				iv_star_9.setImageResource(R.drawable.img_star);
			case 8:
				iv_star_8.setImageResource(R.drawable.img_star);
			case 7:
				iv_star_7.setImageResource(R.drawable.img_star);
			case 6:
				iv_star_6.setImageResource(R.drawable.img_star);
			case 5:
				iv_star_5.setImageResource(R.drawable.img_star);
			case 4:
				iv_star_4.setImageResource(R.drawable.img_star);
			case 3:
				iv_star_3.setImageResource(R.drawable.img_star);
			case 2:
				iv_star_2.setImageResource(R.drawable.img_star);
			case 1:
				iv_star_1.setImageResource(R.drawable.img_star);
				break;
			case 0:
				iv_star_1.setImageResource(R.drawable.img_star_bc);
				iv_star_2.setImageResource(R.drawable.img_star_bc);
				iv_star_3.setImageResource(R.drawable.img_star_bc);
				iv_star_4.setImageResource(R.drawable.img_star_bc);
				iv_star_5.setImageResource(R.drawable.img_star_bc);
				iv_star_6.setImageResource(R.drawable.img_star_bc);
				iv_star_7.setImageResource(R.drawable.img_star_bc);
				iv_star_8.setImageResource(R.drawable.img_star_bc);
				iv_star_9.setImageResource(R.drawable.img_star_bc);
				iv_star_10.setImageResource(R.drawable.img_star_bc);
				break;
			}
		}

	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.activity_step);

		if (StepSettingsActivity.sharedPreferences == null) {
			StepSettingsActivity.sharedPreferences = this.getSharedPreferences(
					StepSettingsActivity.SETP_SHARED_PREFERENCES,
					Context.MODE_PRIVATE);
		}

		if (thread == null) {

			thread = new Thread() {// 子线程用于监听当前步数的变化

				@Override
				public void run() {
					// TODO Auto-generated method stub
					super.run();
					int temp = 0;
					while (true) {
						try {
							Thread.sleep(300);
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						if (StepService.FLAG) {
							Message msg = new Message();
							if (temp != StepDetector.CURRENT_SETP) {
								temp = StepDetector.CURRENT_SETP;
							}
							if (startTimer != System.currentTimeMillis()) {
								timer = tempTime + System.currentTimeMillis()
										- startTimer;
							}
							handler.sendMessage(msg);// 通知主线程
						}
					}
				}
			};
			thread.start();
		}
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();

		// 获取界面控件
		addView();

		// 初始化控件
		init();

	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	/**
	 * 获取Activity相关控件
	 */
	private void addView() {
		tv_show_step = (TextView) this.findViewById(R.id.show_step);
		tv_week_day = (TextView) this.findViewById(R.id.week_day);
		tv_date = (TextView) this.findViewById(R.id.date);

		tv_timer = (TextView) this.findViewById(R.id.timer);

		tv_distance = (TextView) this.findViewById(R.id.distance);
		tv_calories = (TextView) this.findViewById(R.id.calories);
		tv_velocity = (TextView) this.findViewById(R.id.velocity);

		btn_start = (Button) this.findViewById(R.id.start);
		btn_stop = (Button) this.findViewById(R.id.stop);

		// 星标
		iv_star_1 = (ImageView) this.findViewById(R.id.iv_1);
		iv_star_2 = (ImageView) this.findViewById(R.id.iv_2);
		iv_star_3 = (ImageView) this.findViewById(R.id.iv_3);
		iv_star_4 = (ImageView) this.findViewById(R.id.iv_4);
		iv_star_5 = (ImageView) this.findViewById(R.id.iv_5);
		iv_star_6 = (ImageView) this.findViewById(R.id.iv_6);
		iv_star_7 = (ImageView) this.findViewById(R.id.iv_7);
		iv_star_8 = (ImageView) this.findViewById(R.id.iv_8);
		iv_star_9 = (ImageView) this.findViewById(R.id.iv_9);
		iv_star_10 = (ImageView) this.findViewById(R.id.iv_10);
	}

	/**
	 * 初始化界面
	 */
	private void init() {

		step_length = StepSettingsActivity.sharedPreferences.getInt(
				StepSettingsActivity.STEP_LENGTH_VALUE, 70);
		weight = StepSettingsActivity.sharedPreferences.getInt(
				StepSettingsActivity.WEIGHT_VALUE, 50);

		countDistance();
		countStep();
		if ((timer += tempTime) != 0 && distance != 0.0) {

			// 体重、距离
			// 跑步热量（kcal）＝体重（kg）×距离（公里）×1.036
			calories = weight * distance * 0.001;

			velocity = distance * 1000 / timer;
		} else {
			calories = 0.0;
			velocity = 0.0;
		}

		tv_timer.setText(getFormatTime(timer + tempTime));

		tv_distance.setText(formatDouble(distance));
		tv_calories.setText(formatDouble(calories));
		tv_velocity.setText(formatDouble(velocity));

		tv_show_step.setText(total_step + "");

		btn_start.setEnabled(!StepService.FLAG);
		btn_stop.setEnabled(StepService.FLAG);

		if (StepService.FLAG) {
			btn_stop.setText(getString(R.string.pause));
		} else if (StepDetector.CURRENT_SETP > 0) {
			btn_stop.setEnabled(true);
			btn_stop.setText(getString(R.string.cancel));
		}

		setDate();
	}

	/**
	 * 设置显示的日期
	 */
	private void setDate() {
		Calendar mCalendar = Calendar.getInstance();// 获取当天Calendar对象
		int weekDay = mCalendar.get(Calendar.DAY_OF_WEEK);// 当天的星期
		int month = mCalendar.get(Calendar.MONTH) + 1;// 当前月份
		int day = mCalendar.get(Calendar.DAY_OF_MONTH);// 当前日期

		tv_date.setText(month + getString(R.string.month) + day
				+ getString(R.string.day));// 显示当前日期

		String week_day_str = new String();
		switch (weekDay) {
		case Calendar.SUNDAY:// 星期天
			week_day_str = getString(R.string.sunday);
			break;

		case Calendar.MONDAY:// 星期一
			week_day_str = getString(R.string.monday);
			break;

		case Calendar.TUESDAY:// 星期二
			week_day_str = getString(R.string.tuesday);
			break;

		case Calendar.WEDNESDAY:// 星期三
			week_day_str = getString(R.string.wednesday);
			break;

		case Calendar.THURSDAY:// 星期四
			week_day_str = getString(R.string.thursday);
			break;

		case Calendar.FRIDAY:// 星期五
			week_day_str = getString(R.string.friday);
			break;

		case Calendar.SATURDAY:// 星期六
			week_day_str = getString(R.string.saturday);
			break;
		}
		tv_week_day.setText(week_day_str);
	}

	/**
	 * 计算并格式化doubles数值，保留两位有效数字
	 * 
	 * @param doubles
	 * @return 返回当前路程
	 */
	private String formatDouble(Double doubles) {
		DecimalFormat format = new DecimalFormat("####.##");
		String distanceStr = format.format(doubles);
		return distanceStr.equals(getString(R.string.zero)) ? getString(R.string.double_zero)
				: distanceStr;
	}

	public void onClick(View view) {
		Intent service = new Intent(this, StepService.class);
		switch (view.getId()) {
		case R.id.start:
			startService(service);
			btn_start.setEnabled(false);
			btn_stop.setEnabled(true);
			btn_stop.setText(getString(R.string.pause));
			startTimer = System.currentTimeMillis();
			tempTime = timer;
			break;

		case R.id.stop:
			stopService(service);
			if (StepService.FLAG && StepDetector.CURRENT_SETP > 0) {
				btn_stop.setText(getString(R.string.cancel));
			} else {
				StepDetector.CURRENT_SETP = 0;
				tempTime = timer = 0;

				btn_stop.setText(getString(R.string.pause));
				btn_stop.setEnabled(false);

				tv_timer.setText(getFormatTime(timer));

				tv_show_step.setText("0");
				tv_distance.setText(formatDouble(0.0));
				tv_calories.setText(formatDouble(0.0));
				tv_velocity.setText(formatDouble(0.0));

				handler.removeCallbacks(thread);
			}
			btn_start.setEnabled(true);
			break;
		}

	}

	/**
	 * 得到一个格式化的时间
	 * 
	 * @param time
	 *            时间 毫秒
	 * @return 时：分：秒：毫秒
	 */
	private String getFormatTime(long time) {
		// long millisecond = time % 1000;
		time = time / 1000;
		long second = time % 60;
		long minute = (time % 3600) / 60;
		long hour = time / 3600;

		// 毫秒秒显示两位
		// String strMillisecond = "" + (millisecond / 10);
		// 秒显示两位
		String strSecond = ("00" + second)
				.substring(("00" + second).length() - 2);
		// 分显示两位
		String strMinute = ("00" + minute)
				.substring(("00" + minute).length() - 2);
		// 时显示两位
		String strHour = ("00" + hour).substring(("00" + hour).length() - 2);

		return strHour + ":" + strMinute + ":" + strSecond;
		// + strMillisecond;
	}

	/**
	 * 计算行走的步数
	 */
	private void countDistance() {
		if (StepDetector.CURRENT_SETP % 2 == 0) {
			distance = (StepDetector.CURRENT_SETP / 2) * 3 * step_length * 0.01;
		} else {
			distance = ((StepDetector.CURRENT_SETP / 2) * 3 + 1) * step_length
					* 0.01;
		}
	}

	/**
	 * 实际的步数
	 */
	private void countStep() {
		if (StepDetector.CURRENT_SETP % 2 == 0) {
			total_step = StepDetector.CURRENT_SETP / 2 * 3;
		} else {
			total_step = StepDetector.CURRENT_SETP / 2 * 3 + 1;
		}
	}
}
